let common = require("./function/common");
let $ = new common.env('京喜购物返红包助力');
let min = 5,
    help = $.config[$.filename(__filename)] || Math.min(min, $.config.JdMain) || min;
$.setOptions({
    headers: {
        'content-type': 'application/json',
        'user-agent': 'jdapp;iPhone;9.4.6;14.2;965af808880443e4c1306a54afdd5d5ae771de46;network/wifi;supportApplePay/0;hasUPPay/0;hasOCPay/0;model/iPhone8,4;addressid/;supportBestPay/0;appBuild/167618;jdSupportDarkMode/0;Mozilla/5.0 (iPhone; CPU iPhone OS 14_2 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148;supportJDSHWK/1',
        'referer': 'https://happy.m.jd.com/babelDiy/Zeus/3ugedFa7yA6NhxLN5gw2L3PF9sQC/index.html?asid=287215626&un_area=12_904_905_57901&lng=117.612969135975&lat=23.94014745198865',
    }
});
$.readme = `
在京喜下单,如订单有购物返现,脚本会自动查询返现groupid并予以助力,目前每个账号每天能助力3次
44 */6 * * * task ${$.runfile}
如没有订单有购物返现，就不会显示groupid

☆可添加指定的GroupID,填入变量JX_CASHBACK_CODES即可，使用@分隔，优先助力.内容格式 GroupID1@GroupID2@GroupID3
☆可指定需要助力账号,名称变量cashback,内容格式 pt_pin=账号1;pt_pin=账号2;pt_pin=账号3;
`
const pin = process.env.cashback
if (!process.env.cashback) {
    console.log("请在环境变量cashback中填写需要助力的账号pt_pin=")
} else {
    const pin = process.env.cashback
    console.log(`环境变量设置助力${pin}`)
}
if (process.env.JX_CASHBACK_CODES){
	if (process.env.JX_CASHBACK_CODES.length>5){
        let tmpjsonShareCodeArr = process.env.JX_CASHBACK_CODES.split('@');
        for (let op = 0; op < tmpjsonShareCodeArr.length; op++) {
            $.sharecode.push({
                'groupid': tmpjsonShareCodeArr[op]
            })
            console.log(`添加本地groupid：${tmpjsonShareCodeArr[op]}成功`)
        }
	}
}
eval(common.eval.mainEval($));
async function prepare() {
    cookies1 = cookies['help']
    for (let i = 0; i < cookies1.length; i++) {
        let j = cookies1[i]
        let url = `https://wq.jd.com/bases/orderlist/list?order_type=3&start_page=1&last_page=0&page_size=10&callersource=newbiz&t=${$.timestamp}&traceid=&g_ty=ls&g_tk=606717070`
        $.setCookie(j);
        var ptpin = j.match(/pt_pin=([^; ]+)(?=;?)/)[0]
        if (pin.indexOf(ptpin) != -1) {
            await $.curl(url)
            try {
                for (let k of $.source.orderList) {
                    try {
                        let orderid = k.parentId != '0' ? k.parentId : k.orderId
                        let url = `https://wq.jd.com/fanxianzl/zhuli/QueryGroupDetail?isquerydraw=1&orderid=${orderid}&groupid=&sceneval=2&g_login_type=1&g_ty=ls`
                        let dec = await jxAlgo.dec(url)
                        await $.curl(dec.url)
                        let now = parseInt(new Date() / 1000)
                        let end = $.source.data.groupinfo.end_time
                        if (end > now && $.source.data.groupinfo.openhongbaosum != $.source.data.groupinfo.totalhongbaosum) {
                            let groupid = $.source.data.groupinfo.groupid;
                            $.sharecode.push({
                                'groupid': groupid
                            })
                            console.log(`账号${ptpin}--groupid：{$.source.data.groupinfo.groupid}加入助力`)
                        } else {
                            console.log(`账号${ptpin}--groupid：${$.source.data.groupinfo.groupid}已领跳过`)
                        }
                    } catch (e) { }
                }
            } catch (e) { }
        }
    }
}
async function main(id) {
    common.assert(id.groupid, '没有可助力ID')
    let url = `http://wq.jd.com/fanxianzl/zhuli/Help?groupid=${id.groupid}&_stk=groupid&_ste=2&g_ty=ls&g_tk=1710198667&sceneval=2&g_login_type=1`
    let dec = await jxAlgo.dec(url)
    await $.curl(dec.url)
    console.log($.source.data.prize.discount)
}
